<template>
	<view class="content">
		<button class="size" @tap="getConst">获取通讯录</button>
		<button class="size" @tap="getAddressBook">获取通讯录对象</button>
		<button class="size" @tap="create">添加联系人</button>
		<button class="size" @tap="find">查找联系人</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cmr: null
			}
		},
		onLoad() {

		},
		methods: {
			getConst() {
				// 用于获取系统的联系人信息
				const ADDRESSBOOK_PHONE = plus.contacts.ADDRESSBOOK_PHONE;
				console.log("ADDRESSBOOK_PHONE=>", ADDRESSBOOK_PHONE)
				// 用于获取SIM卡上的联系人信息
				const ADDRESSBOOK_SIM = plus.contacts.ADDRESSBOOK_SIM;
				console.log("ADDRESSBOOK_SIM=>", ADDRESSBOOK_SIM)
			},
			getAddressBook() {
				plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
					// 可通过addressbook进行通讯录操作
					console.log("Get address book success!");
				}, function(e) {
					console.log("Get address book failed: " + e.message);
				});
			},
			create() {
				plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
					// 可通过addressbook进行通讯录操作
					console.log("Get address book success!");
					// 向通讯录中添加联系人
					let contact = addressbook.create();
					contact.name = {
						givenName: "王安"
					};
					contact.phoneNumbers = [{
						type: "手机",
						value: "88888888",
						preferred: true
					}];
					contact.save();
				}, function(e) {
					console.log("Get address book failed: " + e.message);
				});
			},
			find(){
				plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
					// 可通过addressbook进行通讯录操作
					console.log("Get address book success!");
					// 向通讯录中添加联系人
					addressbook.find(["displayName","phoneNumbers"],function(contacts){
						console.log("contacts=>",contacts)
					},function(error){
						console.log("error=>",error)
					},{multiple:true});
					
				}, function(e) {
					console.log("Get address book failed: " + e.message);
				});
			}

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.size {
		margin: 30rpx 0;
	}
</style>
